package com.example.servicetest

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button

class MainActivity : AppCompatActivity() {
    lateinit var downloadBinder: MyService.DownloadBinder
    private val connection = object : ServiceConnection{
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            downloadBinder = service as MyService.DownloadBinder
            downloadBinder.startDownload()
            downloadBinder.getProgress()

        }

        override fun onServiceDisconnected(name: ComponentName?) {

        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val startServiceBtn: Button = findViewById(R.id.startServiceBtn)
        startServiceBtn.setOnClickListener{
            val intent = Intent(this,MyService::class.java)
            startService(intent)
        }
        val stopServiceBtn: Button = findViewById(R.id.stopServiceBtn)
        stopServiceBtn.setOnClickListener {
            val intent = Intent(this,MyService::class.java)
            stopService(intent)
        }
        val bindServiceBtn: Button = findViewById(R.id.bindServiceBtn)
        bindServiceBtn.setOnClickListener {
            val intent = Intent(this,MyService::class.java)
            bindService(intent,connection, Context.BIND_AUTO_CREATE)
        }
        val unbindServiceBtn: Button = findViewById(R.id.unbindServiceBtn)
        unbindServiceBtn.setOnClickListener {
            unbindService(connection)
        }
        val startIntentServiceBtn: Button = findViewById(R.id.startIntentServiceBtn)
        startIntentServiceBtn.setOnClickListener {
            Log.d("MainActivity","Thread id is ${Thread.currentThread().name}")
            val intent = Intent(this,MyIntentService::class.java)
            startService(intent)
        }
    }
}